

#include "mpi_lib.h"
#include "mdi_common.h"
#include "sample_tool.h"

static int  g_type = -1;

#define REG_READ   (1)
#define REG_WRITE  (2)
#define MEM_DUMP  (3)
#define CAP_LOG  (4)

static int g_fw_log_fd 	= -1;
static u8 *g_log_mem_addr = NULL;
static int g_log_mem_size	= 0;


int main(int argc, char **argv[])
{

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);

    sgks_cmd_dump_mem_file_s tmp_dump;
    sgks_cmd_dump_mem_file_s *drv_dump = NULL;

    sgks_mdi_reg_rw_s tmp_reg;
    sgks_mdi_reg_rw_s *drv_reg = NULL;

    sgks_cap_dsp_log_file_s tmp_log;
    sgks_cap_dsp_log_file_s *drv_log;
    sgks_mdi_mmap_s *mmap_info = NULL;

    int fd = -1;
    u8 sbuff[128];

    if (argc < 4)
    {
        printf("./sgks_debug_tool <mem> <ddr addr> <dump size> <save_file>\n");
        printf("./sgks_debug_tool <reg> <r> <reg addr>\n");
        printf("./sgks_debug_tool <reg> <w> <reg addr> <reg value>\n");
        return -1;
    }

    if (strcmp(argv[1], "reg") == 0)
    {

        if (strcmp(argv[2], "r") == 0)
        {
            if (argc != 4)
            {
                Printf("reg addr err!\n");
                return -1;
            }

            g_type = REG_READ;

            memset(&tmp_reg, 0, sizeof(sgks_mdi_reg_rw_s));

            sscanf(argv[3],"%x",&tmp_reg.reg_info[0].addr);
             
        }
        else if (strcmp(argv[2], "w") == 0)
        {

            if (argc != 5)
            {
                Printf("write addr err!\n");
                return -1;
            }

            g_type = REG_WRITE;

            memset(&tmp_reg, 0, sizeof(sgks_mdi_reg_rw_s));
            sscanf(argv[3], "%x", &tmp_reg.reg_info[0].addr);
            sscanf(argv[4], "%x", &tmp_reg.reg_info[0].val);

        }


    }
    else if (strcmp(argv[1], "mem") == 0)
    {

        if (argc != 5)
        {
            Printf("mem err!\n");
            return -1;
        }

        g_type = MEM_DUMP;

        memset(&tmp_dump, 0, sizeof(sgks_cmd_dump_mem_file_s));


        sscanf(argv[2],"%x",&tmp_dump.addr);

        sscanf(argv[3],"%x",&tmp_dump.len);
        

        strcpy(tmp_dump.file_path, argv[4]);


    }
    else if (strcmp(argv[1], "log") == 0)
    {

        if (argc != 5)
        {
            Printf("log err!\n");
            return -1;
        }

        g_type = CAP_LOG;

        memset(&tmp_log, 0, sizeof(sgks_cap_dsp_log_file_s));


        sscanf(argv[2],"%x",&tmp_log.log_mem_addr);
        //printf("argv[2]=%s, addr=0x%x\n",argv[2],tmp_log.log_mem_addr);


        sscanf(argv[3],"%x",&tmp_log.log_mem_size);
        
        strcpy(tmp_log.log_file_path, argv[4]);

    }


    if((fd = open(MPI_DRIVER_MEDIA_DEV, O_RDWR, 0)) < 0)
    {
        Printf("err open mpi driver dev:%s\n", MPI_DRIVER_MEDIA_DEV);
        return SGKS_ERR_SYS_OPENDRIVERDEVERR;
    }

    switch(g_type)
    {

    case REG_READ:

        drv_reg					        = msg_buf;
        drv_reg->reg_num 				= 1;
        drv_reg->reg_info[0].addr       = tmp_reg.reg_info[0].addr;
        drv_reg->reg_info[0].addr_type  = SGKS_MDI_REG_ADDR_TYPE_PHY;
        SGKS_MDI_DRV_IOCTL_EX(fd, SGKS_MDI_OPERATION_DEBUG_REGREAD, 0, sizeof(sgks_mdi_reg_rw_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

        break;

    case REG_WRITE:
        drv_reg					        = msg_buf;
        drv_reg->reg_num 				= 1;
        drv_reg->reg_info[0].addr 		= tmp_reg.reg_info[0].addr;
        drv_reg->reg_info[0].addr_type  = SGKS_MDI_REG_ADDR_TYPE_PHY;
        drv_reg->reg_info[0].val  		= tmp_reg.reg_info[0].val;
        SGKS_MDI_DRV_IOCTL_EX(fd, SGKS_MDI_OPERATION_DEBUG_REGWRITE, 0, sizeof(sgks_mdi_reg_rw_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
        break;


    case CAP_LOG:
        memset(sbuff, 0, sizeof(sbuff));
        sprintf(sbuff, "rm -rf %s", tmp_log.log_file_path);
        system(sbuff);

        memset(msg_buf, 0, sizeof(sgks_cap_dsp_log_file_s));
        drv_log					        = msg_buf;
        drv_log->log_mem_addr			= tmp_log.log_mem_addr;
        drv_log->log_mem_size			= tmp_log.log_mem_size;
        //drv_log->log_callback			= (LOG_CB_FUN)0xF400B000;
        sprintf(drv_log->log_file_path, tmp_log.log_file_path);
        SGKS_MDI_DRV_IOCTL_EX(fd, SGKS_MDI_OPERATION_DEBUG_CAPDSPLOG, 0, sizeof(sgks_cap_dsp_log_file_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

        break;

    case MEM_DUMP:

        memset(sbuff, 0, sizeof(sbuff));
        sprintf(sbuff, "rm -rf %s", tmp_dump.file_path);
        system(sbuff);

        memset(msg_buf, 0, sizeof(sgks_cmd_dump_mem_file_s));
        drv_dump = msg_buf;
        drv_dump->addr                     = tmp_dump.addr;
        drv_dump->len                      = tmp_dump.len;
        sprintf(drv_dump->file_path, tmp_dump.file_path);
        SGKS_MDI_DRV_IOCTL_EX(fd, SGKS_MDI_OPERATION_DEBUG_DUMPMEMTOFILE, 0, sizeof(sgks_cmd_dump_mem_file_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

        memset(sbuff, 0, sizeof(sbuff));
        sprintf(sbuff, "chmod -R 777 %s", tmp_dump.file_path);
        system(sbuff);

        break;

    default:
        Printf("tool fail!\n");
        break;

    }

    close(fd);


    return 0;

}




